<?php
include("../include/dbcommon.php");
define ("JAVA_HOSTS", $_SERVER["HTTP_HOST"].":8080");
define ("JAVA_SERVLET", "/JavaBridge/JavaBridge.phpjavabridge");
$pth = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$path_parts = pathinfo($pth);
$imageURLPrefix = $path_parts['dirname'] ."/sessionChartImages/";


require_once("http://127.0.0.1:8080/JavaBridge/java/Java.inc");

function getOutputFormat($format,$output_file_name="output") {
	$fmt = null;

	switch($format) {
	case "pdf": 
		$fmt = new java("org.eclipse.birt.report.engine.api.PDFRenderOption");
		$fmt->setOutputFormat("pdf");
		header("Content-type: application/pdf");
		
		break;
	case "html": 
		$fmt = new java("org.eclipse.birt.report.engine.api.HTMLRenderOption");
		$fmt->setOutputFormat("html");
		$ih = new java( "org.eclipse.birt.report.engine.api.HTMLServerImageHandler");
		$fmt->setImageHandler($ih);
		header("Content-type: text/html");
		break;
	case "doc":
		$fmt = new java("org.eclipse.birt.report.engine.api.RenderOption");
		$fmt->setOutputFormat("doc");
		header("Content-type: application/msword");
		break;
	case "xls":
		$fmt = new java("org.eclipse.birt.report.engine.api.RenderOption");
		$fmt->setOutputFormat("xls");
		header("Content-type: application/vnd.ms-excel");
		break;
	default: die("unknown output format $format");
	}
	//if($format).
	header("Content-Disposition: inline; filename=".$output_file_name.".".$format);
	return $fmt;
}

$myFormat=$_GET['output_format']!=""?$_GET['output_format']:'pdf';
$id=$_GET['id'];
$pageId=$_GET['page_id'];
$strSQL="SELECT * FROM reports WHERE id = $pageId;";
$rs=db_query($strSQL,$conn);
$data=db_fetch_array($rs);

$file_name=$data['file_name'].".rptdesign";
$output_file_name=$data['file_name']."_".$id;
$xml = simplexml_load_file("source/".$file_name);

$host_regen=$host;
$sys_dbname_regen=$sys_dbname;
$user_regen=$user;
$port_regen=$port!=""?":".$port:"";
$pwd_regen=$pwd!=""?base64_encode($pwd):"";

$xml->{'data-sources'}->{'oda-data-source'}->{'property'}[1]="jdbc:mysql://".$host_regen.$port_regen."/".$sys_dbname_regen;
$xml->{'data-sources'}->{'oda-data-source'}->{'property'}[2]=$user_regen;
if($pwd_regen!=''){
	$xml->{'data-sources'}->{'oda-data-source'}->{'encrypted-property'}=$pwd_regen;
}
$xmlstring=$xml->asXML();
$file_name_tmp=date('Ymdhmi').'_'.$file_name; 
$fp = fopen('tmp/'.$file_name_tmp, 'w');
fwrite($fp,$xmlstring);
fclose($fp);

$here = getcwd();

$ctx = java_context()->getServletContext();
$birtReportEngine =java("org.eclipse.birt.php.birtengine.BirtEngine")->getBirtEngine($ctx);
java_context()->onShutdown(java("org.eclipse.birt.php.birtengine.BirtEngine")->getShutdownHook());
try{

	$report = $birtReportEngine->openReportDesign("${here}\\tmp\\".$file_name_tmp);
	$task = $birtReportEngine->createRunAndRenderTask($report);
	$task->setParameterValue("id", $id);
	$fmt = getOutputFormat($myFormat,$output_file_name);
	$fmt->setOutputStream($out=new java("java.io.ByteArrayOutputStream"));
	$task->setRenderOption($fmt);
	$task->run();
	$task->close();

}
catch (JavaException $e) {
	 echo $e;
}
unlink("${here}\\tmp\\".$file_name_tmp);
echo java_values($out->toByteArray());



?>
